package com.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.InstrumentBean;
import com.bean.MessageBean;
import com.dao.InstrumentDao;
import com.exception.DBException;
import com.exception.InstrumentException;

public class InstrumentAction
{
	public void addInstrument(HttpServletRequest request,
			HttpServletResponse response, InstrumentBean insBean)
			throws DBException, InstrumentException
	{
		InstrumentDao insDao = new InstrumentDao();

		if (insDao.ifInsExist(insBean))
		{
			throw new InstrumentException("Instrument barcode is already existed");
		}

		insDao.addInstruments(insBean);
		insDao.getInstrumentsDetail(insBean);
		request.setAttribute("insDetail", insBean);

		try
		{
			request.getRequestDispatcher("/lab_admin/instrument_detail.jsp")
					.forward(request, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private ArrayList<InstrumentBean> query_Key_Value(MessageBean message)
			throws DBException, InstrumentException
	{
		InstrumentBean insBean = new InstrumentBean();
		InstrumentDao insDao = new InstrumentDao();
		ArrayList<InstrumentBean> insList = null;
		String key = message.getKey();
		String value = message.getValue();
		// query instrument by barcode
		if ("barcode".equals(key))
		{
			insBean.setBarcode(value);
			insList = insDao.getInstrumentsByBarcode(insBean);
		}
		// query instrument by name
		else if ("name".equals(key))
		{
			insBean.setName(value);
			insList = insDao.getInstrumentsByName(insBean);
		}
		return insList;
	}

	public void queryInstrument(HttpServletRequest request,
			HttpServletResponse response, MessageBean message)
			throws DBException, InstrumentException
	{
		ArrayList<InstrumentBean> insList = null;

		insList = query_Key_Value(message);

		request.setAttribute("insList", insList);
		request.setAttribute("message", message);
		try
		{
			request.getRequestDispatcher("/lab_admin/instrument.jsp").forward(
					request, response);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void detailInstrument(HttpServletRequest request,
			HttpServletResponse response, InstrumentBean insBean)
			throws DBException, InstrumentException
	{
		InstrumentDao insDao = new InstrumentDao();
		if (!insDao.ifInsExist(insBean))
		{
			throw new InstrumentException("Instrument is not exist");
		}
		insDao.getInstrumentsDetail(insBean);
		request.setAttribute("insDetail", insBean);
		try
		{
			request.getRequestDispatcher("/lab_admin/instrument_detail.jsp")
					.forward(request, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void deleteInstrument(HttpServletRequest request,
			HttpServletResponse response, InstrumentBean insBean,
			MessageBean message) throws DBException, InstrumentException
	{
		
		InstrumentDao insDao = new InstrumentDao();
		if (!insDao.ifInsExist(insBean))
		{
			throw new InstrumentException("Instrument is not exist");
		}
		ArrayList<InstrumentBean> insList = null;
		insDao.deleteInstruments(insBean);
		insList = query_Key_Value(message);

		request.setAttribute("insList", insList);
		request.setAttribute("message", message);

		try
		{
			request.getRequestDispatcher("/lab_admin/instrument.jsp").forward(
					request, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void modifyInstrument_Edit(HttpServletRequest request,
			HttpServletResponse response, InstrumentBean insBean)
			throws DBException, InstrumentException
	{
		InstrumentDao insDao = new InstrumentDao();
		if (!insDao.ifInsExist(insBean))
		{
			throw new InstrumentException("Instrument is not exist");
		}
		insDao.getInstrumentsDetail(insBean);
		request.setAttribute("insDetail", insBean);
		try
		{
			request.getRequestDispatcher("/lab_admin/instrument_modify.jsp")
					.forward(request, response);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void modifyInstrument(HttpServletRequest request,
			HttpServletResponse response, InstrumentBean insBean)
			throws DBException, InstrumentException
	{
		InstrumentDao insDao = new InstrumentDao();
		if (!insDao.ifInsExist(insBean))
		{
			throw new InstrumentException("Instrument is not exist");
		}
		insDao.updateInstruments(insBean);
		insDao.getInstrumentsDetail(insBean);
		request.setAttribute("insDetail", insBean);
		try
		{
			request.getRequestDispatcher("/lab_admin/instrument_detail.jsp")
					.forward(request, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
